package cn.anc.aonicardv.module.adpter.album;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.GlideApp;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.bean.PhotoBean;
import cn.anc.aonicardv.event.AllSelectEvent;
import cn.anc.aonicardv.event.EditEvent;
import cn.anc.aonicardv.event.SelectEvent;
import cn.anc.aonicardv.module.adpter.listener.OnAlbumItemClickListener;
import cn.anc.aonicardv.module.adpter.listener.OnAlbumItemLongClickListener;
import cn.anc.aonicardv.module.ui.album.PhotoFragment;
import cn.anc.aonicardv.util.AoniBeanDecorationUtils;

/**
 * Created by yangdai on 2017/5/19.
 */

public class PhotoAdapter extends RecyclerView.Adapter {

    private List<PhotoBean> photoBeanList;
    private boolean isEditMode;
    private PhotoFragment fragment;

    public PhotoAdapter(PhotoFragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        if (AoniBeanDecorationUtils.getDirectionByItemPosition(fragment.decorationBeanList, position) == Constant.DecorationParam.LEFT || AoniBeanDecorationUtils.getDirectionByItemPosition(fragment.decorationBeanList, position) == Constant.DecorationParam.RIGHT) {
            GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            layoutParams.width = fragment.getResources().getDisplayMetrics().widthPixels / 3;
            layoutParams.height = fragment.getResources().getDisplayMetrics().widthPixels / 3*9/16;

            layoutParams.leftMargin = 0;
            layoutParams.rightMargin = 0;
            layoutParams.bottomMargin = 5;
        } else {
            GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            layoutParams.width = (fragment.getResources().getDisplayMetrics().widthPixels / 3) - 10;
            layoutParams.height = fragment.getResources().getDisplayMetrics().widthPixels / 3*9/16;
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 5;
            layoutParams.bottomMargin = 5;
        }
        GlideApp.with(fragment.getContext()).load(Uri.fromFile( new File( photoBeanList.get(position).getPath()))).placeholder(R.mipmap.image_loading).into( viewHolder.photoIv);
        String resolution = photoBeanList.get(position).getResolution();
        String fileCreateTime = photoBeanList.get(position).getFileCreateTime();
        if (resolution != null) {
            String sub_res = resolution.substring(resolution.lastIndexOf("x") + 1, resolution.length()) + "P";
            viewHolder.resolutionTv.setVisibility(View.VISIBLE);
            viewHolder.resolutionTv.setText(sub_res);
        } else
            viewHolder.resolutionTv.setVisibility(View.GONE);

        if (fileCreateTime != null) {
            String sub_time = fileCreateTime.substring(fileCreateTime.indexOf(" "), fileCreateTime.length());
            viewHolder.timeTv.setVisibility(View.VISIBLE);
            viewHolder.timeTv.setText(sub_time);
        } else
            viewHolder.timeTv.setVisibility(View.GONE);
        viewHolder.selectCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (viewHolder.selectCb.isPressed()) {
                    photoBeanList.get(position).setCheck(isChecked);
                    EventBus.getDefault().post(new SelectEvent(isChecked, photoBeanList.size()));
                }
            }
        });
        if (photoBeanList.get(position).isCheck()) {
            viewHolder.selectCb.setChecked(true);
            viewHolder.mask.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selectCb.setChecked(false);
            viewHolder.mask.setVisibility(View.GONE);
        }
        if (isEditMode) {
            viewHolder.selectCb.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selectCb.setChecked(false);
            viewHolder.selectCb.setVisibility(View.GONE);
        }
        viewHolder.photoIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEditMode) {
                    if (viewHolder.mask.getVisibility() == View.GONE)
                        viewHolder.mask.setVisibility(View.VISIBLE);
                    else
                        viewHolder.mask.setVisibility(View.GONE);
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(viewHolder.selectCb, position, isEditMode);
                }
            }
        });
        viewHolder.photoIv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null)
                    onItemLongClickListener.onItemLongClick(v, position, isEditMode);
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return photoBeanList == null ? 0 : photoBeanList.size();
    }

    public void setData(List<PhotoBean> data) {
        this.photoBeanList = data;
        notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EditEvent event) {
        if (!fragment.getUserVisibleHint())
            return;
        this.isEditMode = event.isEdit;
        notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AllSelectEvent event) {
        if (!fragment.getUserVisibleHint())
            return;
        for (PhotoBean p : photoBeanList
                ) {
            p.setCheck(event.isAllSelect);
        }
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.iv_photo)
        public ImageView photoIv;
        @BindView(R2.id.tv_resolution)
        public TextView resolutionTv;
        @BindView(R2.id.tv_time)
        public TextView timeTv;
        @BindView(R2.id.cb_select)
        public CheckBox selectCb;
        @BindView(R2.id.mask)
        View mask;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private OnAlbumItemClickListener onItemClickListener;
    private OnAlbumItemLongClickListener onItemLongClickListener;

    public void setOnItemClickListener(OnAlbumItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnAlbumItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void registerEventBusForAdapter(){
        EventBus.getDefault().register(this);
    }

    public void unregisterEventBusForAdapter(){
        EventBus.getDefault().unregister(this);
    }
}
